package com.mmc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.mmc.dto.UserInfoDTO;
import com.mmc.entity.UserInfo;
import com.mmc.exception.Assert;
import com.mmc.mapper.UserInfoMapper;
import com.mmc.result.ResponseEnum;
import com.mmc.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mmc.utils.JwtUtils;
import com.mmc.utils.MD5;
import com.mmc.vo.UserLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户基本信息 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-20
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    @Override
    public UserInfoDTO login(UserLoginVO loginVO) {
        Assert.notNull(loginVO.getMobile(), ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notNull(loginVO.getPassword(),ResponseEnum.PASSWORD_NULL_ERROR);
        UserInfo userInfo = getOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getMobile, loginVO.getMobile())
                .eq(UserInfo::getUserType, loginVO.getUserType()));
        Assert.notNull(userInfo,ResponseEnum.LOGIN_MOBILE_ERROR);

        Assert.equals(MD5.encrypt(loginVO.getPassword()) ,userInfo.getPassword(),ResponseEnum.LOGIN_PASSWORD_ERROR);
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        BeanUtils.copyProperties(userInfo,userInfoDTO);
        String token = JwtUtils.createToken(userInfo.getId(), userInfo.getName());
        userInfoDTO.setToken(token);
        return userInfoDTO;
    }
}
